
/**   
*    
* 项目名称：a_yongan   
* 类名称：OutsourcingInController   
* 类描述：   
* 创建人：liubo   
* 创建时间：2018年6月19日 下午3:29:51    
* 修改时间：2018年6月19日 下午3:29:51   
* 修改备注：   
* @version    
*    
*/
package com.yongan.production.web;

import java.util.List;
import java.util.UUID;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.yongan.production.entity.ProductStructureList;
import com.yongan.production.service.ProductStructureListService;
import com.yongan.user.dto.UserResult;
import com.yongan.util.Execution;
import com.yongan.util.PageBean;

/**
 * @author liubo
 *
 */
@Controller
@RequestMapping("/productstructurelist")
public class ProductStructureListController {

	@Autowired
	private ProductStructureListService listService;
	
	@RequestMapping(value = "/add", method = RequestMethod.GET)
	public String productstructurelistadd(Model model) {
		/*String number = SerialNumber.Getnum();
		number = "XSD"+number;
		model.addAttribute("number", number);*/
		return "productstructurelistadd";
	}
	@RequestMapping(value = "/adddo", method = RequestMethod.POST, produces = { "application/json;charset=UTF-8" })
	@ResponseBody
	public UserResult<Execution> productstructurelistadddo(ProductStructureList productStructureList,Model model) {
		System.out.println(productStructureList.toString());
		productStructureList.setProductStructureListId(UUID.randomUUID().toString());
		Execution rs = listService.executeProductStructureList(productStructureList, "", "1233213");
		int rsn = rs.getState();
		if (rsn == 1) {
			return new UserResult<Execution>(true, rs);
		}
		return new UserResult<Execution>(false, "");
	}
	
	@RequestMapping(value = "/productstructurelistlist", method = RequestMethod.GET)
	public String productstructurelistcontent(Model model,HttpServletRequest request) {
		List<ProductStructureList> listAll = null;
		String page = request.getParameter("currentPage");
		int currentPage;
		int totalRecords;
		if (page != null) {
			currentPage = Integer.parseInt(page);
		} else {
			currentPage = 1;
		}
		PageBean<ProductStructureList> pb = new PageBean<ProductStructureList>();
		
		totalRecords = listService.getCount();
		pb.setCurrentPage(currentPage);
		pb.setTotalRecords(totalRecords);
		pb.setTotalPages(pb.getTotalRecords() % pb.getPagesize() == 0 ? pb
				.getTotalRecords() / pb.getPagesize() : pb.getTotalRecords()
				/ pb.getPagesize() + 1);
		if (totalRecords > 0) {
			int start = (pb.getCurrentPage() - 1) * pb.getPagesize();
			int end = pb.getCurrentPage() * pb.getPagesize();
			if (end > totalRecords - 1) {
				end = totalRecords;
			}
			listAll = listService.getProductStructureList(start, end);
			//model.addAttribute("pb", pb);
		}
		model.addAttribute("pb", pb);
		model.addAttribute("productstructurelistall", listAll);
		return "productstructurelistlist";
	}
	
	@RequestMapping(value = "/{productstructurelistId}/detail", method = RequestMethod.GET)
	public String productstructurelistdetail(@PathVariable("productstructurelistId") String productstructurelistId, Model model) {
		if (!(productstructurelistId.equals("")|productstructurelistId==null)) {
			ProductStructureList productStructureList = listService.getProductStructureListById(productstructurelistId);
			model.addAttribute("productstructurelist", productStructureList);
		}
		return "productstructurelistdetail";
	}
	
	@RequestMapping(value = "/update", method = RequestMethod.POST, produces = { "application/json;charset=UTF-8" })
	@ResponseBody
	public UserResult<Execution> productstructurelistupdate(ProductStructureList productStructureList, Model model) {
		Execution rs = listService.executeProductStructureList(productStructureList, productStructureList.getProductStructureListId(), "123321231");
		int rsn = rs.getState();
		if (rsn == 1) {
			return new UserResult<Execution>(true, rs);
		}
		return new UserResult<Execution>(false, "");
	}
	
	@RequestMapping(value = "/{productstructurelistId}/delete", method = RequestMethod.GET, produces = { "application/json;charset=UTF-8" })
	@ResponseBody
	public UserResult<Execution> productstructurelistDelete(
			@PathVariable("productstructurelistId") String productstructurelistId, Model model) {
		if (!(productstructurelistId.equals("")|productstructurelistId==null)) {
			Execution rsdelete = listService.executeProductStructureList(null, productstructurelistId, "1212331232");
			int rsn = rsdelete.getState();
			if (rsn == 1) {
				return new UserResult<Execution>(true, rsdelete);
			}
		}
		return new UserResult<Execution>(false, "");
	}
	
}
